﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using KGE.Define;
using Microsoft.DirectX;

namespace KGE.Tools
{
    public class SceneMakerCOM : KGE.GameCOM
    {
        public bool isMovie;
        public KImage mouseKI;
        public KMovie mouseKM;
        public Point mousePoint;
        public Point camera;
        public Scene scene;
        public bool drawMouse = false;
        public Rectangle mouseZone;

        public SceneMakerCOM(Game g)
            : base(g)
        {
            newScene();
        }

        public override void Draw()
        {
            base.Draw();

            drawScene();
            drawZone();

            if (drawMouse)
            {
                if (isMovie)
                {
                    if (mouseKM != null)
                    {
                        Point p = Util.PointSubPoint(mousePoint, camera);
                        game.DrawKMovie(new ObjectIndexer(mouseKM.Name), Color.FromArgb(200, 255, 255, 255), Util.Point2Vector3(p));
                    }
                }
                else
                {
                    if (mouseKI != null)
                    {
                        Point p = Util.PointSubPoint(mousePoint, camera);
                        game.DrawKImage(new ObjectIndexer(mouseKI.Name), Color.FromArgb(200, 255, 255, 255), Util.Point2Vector3(p));
                    }
                }
            }
        }
        public void resetScene()
        {
            for (int i = 0; i < scene.LayerCount; i++)
            {
                for (int j = 0; j < scene[i].TileCount; j++)
                {
                    if (scene[i][j].KImageIndexer != null)
                    {
                        scene[i][j].KImageIndexer.Index = -1;
                    }
                    else
                    {
                        scene[i][j].KMovieIndexer.Index=-1;
                    }
                }
            }
        }
        void drawScene()
        {
            for (int i = 0; i < scene.LayerCount; i++)
            {
                for (int j = 0; j < scene[i].TileCount; j++)
                {
                    Point p = Util.PointSubPoint(scene[i][j].DrawRectangle.Location, camera);
                    Vector3 v3 = Util.Point2Vector3(p);
                    if (scene[i][j].KImageIndexer != null)
                    {
                        game.DrawKImage(scene[i][j].KImageIndexer, Color.White, v3);
                    }
                    else
                    {
                        game.DrawKMovie(scene[i][j].KMovieIndexer, Color.White, v3);
                    }
                }
            }
            for (int i = 0; i < scene.LayerCount; i++)
            {
                for (int j = 0; j < scene[i].TileCount; j++)
                {
                    Point p = Util.PointSubPoint(scene[i][j].DrawRectangle.Location, camera);

                    game.DrawText(j.ToString("d3"), Color.White, Util.Point2Vector3(p));
                }
            }
        }
        void drawZone()
        {
            if (scene.Zones == null)
            {
                scene.Zones = new List<Rectangle>();
            }
            for (int i=0;i<scene.Zones.Count;i++)
            {
                Rectangle r = scene.Zones[i];
                r.X -= camera.X;
                r.Y -= camera.Y;
                game.DrawRectangle(Color.Red, r);
            }
        }

        public void newScene()
        {
            scene = null;
            scene = new Scene();
        }
    }
}
